package com.ruoyi.record.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.record.domain.RecordProfessional;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.service.IRecordProfessionalService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 职称申报Controller
 * 
 * @author ruoyi
 * @date 2024-05-09
 */
@Controller
@RequestMapping("/record/professional")
public class RecordProfessionalController extends BaseController
{
    private String prefix = "record/professional";

    @Autowired
    private IRecordProfessionalService recordProfessionalService;

    @RequiresPermissions("record:professional:view")
    @GetMapping()
    public String professional(ModelMap mmap)
    {
        mmap.put("showFlag", ShiroUtils.getSysUser().isAdmin() || ShiroUtils.getSubject().hasRole("administrator"));
        return prefix + "/professional";
    }

    /**
     * 查询职称申报列表
     */
    @RequiresPermissions("record:professional:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordProfessional recordProfessional)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin() && !ShiroUtils.getSubject().hasRole("administrator")) {
            recordProfessional.setCreateBy(ShiroUtils.getLoginName());
        }
        List<RecordProfessional> list = recordProfessionalService.selectRecordProfessionalList(recordProfessional);
        return getDataTable(list);
    }

    /**
     * 新增职称申报
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存职称申报
     */
    @RequiresPermissions("record:professional:add")
    @Log(title = "职称申报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordProfessional recordProfessional)
    {
        recordProfessional.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recordProfessionalService.insertRecordProfessional(recordProfessional));
    }

    /**
     * 修改职称申报
     */
    @GetMapping("/edit/{professionalId}")
    public String edit(@PathVariable("professionalId") Long professionalId, ModelMap mmap)
    {
        RecordProfessional recordProfessional = recordProfessionalService.selectRecordProfessionalByProfessionalId(professionalId);
        mmap.put("recordProfessional", recordProfessional);
        return prefix + "/edit";
    }

    /**
     * 修改保存职称申报
     */
    @RequiresPermissions("record:professional:edit")
    @Log(title = "职称申报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordProfessional recordProfessional)
    {
        recordProfessional.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recordProfessionalService.updateRecordProfessional(recordProfessional));
    }

    /**
     * 删除职称申报
     */
    @RequiresPermissions("record:professional:remove")
    @Log(title = "职称申报", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordProfessionalService.deleteRecordProfessionalByProfessionalIds(ids));
    }

    /**
     * 职称申报提交
     */
    @RequiresPermissions("record:professional:submit")
    @Log(title = "职称申报提交", businessType = BusinessType.OTHER)
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submit(Long professionalId)
    {
        recordProfessionalService.update(null, new LambdaUpdateWrapper<RecordProfessional>().set(RecordProfessional::getSubmitFlag, "1")
                .set(RecordProfessional::getSubmitTime, DateUtils.getNowDate()).eq(RecordProfessional::getProfessionalId, professionalId));
        return success();
    }

    @RequiresPermissions("verify:professional:view")
    @GetMapping("/verify")
    public String verify()
    {
        return prefix + "/verify/professional";
    }

    /**
     * 查询请假记录列表
     */
    @RequiresPermissions("verify:professional:list")
    @PostMapping("/verify/list")
    @ResponseBody
    public TableDataInfo verifyList(RecordProfessional recordProfessional)
    {
        startPage();
        List<RecordProfessional> list = recordProfessionalService.selectVerifyProfessionalList(recordProfessional);
        return getDataTable(list);
    }

    @Log(title = "职称申报", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecordProfessional> util = new ExcelUtil<RecordProfessional>(RecordProfessional.class);
        List<RecordProfessional> professionalList = BeanUtils.copyList(util.importExcel(file.getInputStream()), RecordProfessional::new);
        String message = recordProfessionalService.importRecordProfessional(professionalList, updateSupport);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecordProfessional> util = new ExcelUtil<RecordProfessional>(RecordProfessional.class);
        return util.importTemplateExcel("职称申报");
    }

    /**
     * 导出职称申报列表
     */
    @Log(title = "职称申报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecordProfessional recordProfessional)
    {
        List<RecordProfessional> list = recordProfessionalService.selectVerifyProfessionalList(recordProfessional);
        ExcelUtil<RecordProfessional> util = new ExcelUtil<RecordProfessional>(RecordProfessional.class);
        return util.exportExcel(list, "职称申报");
    }

    /**
     * 职称申报提交
     */
    @RequiresPermissions("verify:professional:cancel")
    @Log(title = "职称申报撤销", businessType = BusinessType.OTHER)
    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(Long professionalId, String submitRemark)
    {
        recordProfessionalService.update(null, new LambdaUpdateWrapper<RecordProfessional>().set(RecordProfessional::getSubmitFlag, "2")
                .set(RecordProfessional::getSubmitTime, null).set(RecordProfessional::getSubmitRemark, submitRemark).eq(RecordProfessional::getProfessionalId, professionalId));
        return success();
    }

    /**
     * 修改职称申报
     */
    @GetMapping("/verify/{professionalId}")
    public String verify(@PathVariable("professionalId") Long professionalId, ModelMap mmap)
    {
        RecordProfessional recordProfessional = recordProfessionalService.selectRecordProfessionalByProfessionalId(professionalId);
        mmap.put("recordProfessional", recordProfessional);
        return prefix + "/verify/verify";
    }

    /**
     * 修改保存职称申报
     */
    @RequiresPermissions("verify:professional:save")
    @Log(title = "职称申报审批", businessType = BusinessType.UPDATE)
    @PostMapping("/verify/save")
    @ResponseBody
    public AjaxResult verifySave(RecordProfessional recordProfessional)
    {
        return toAjax(recordProfessionalService.verifyRecordProfessional(recordProfessional));
    }

    /**
     * 职称申报得分
     * @param userType
     * @param mmap
     * @return
     */
    @RequiresPermissions("score:professional:view")
    @GetMapping("/score/{userType}")
    public String score(@PathVariable("userType") String userType, ModelMap mmap)
    {
        mmap.put("userType", userType);
        return prefix + "/score/professional";
    }

    /**
     * 查询请假记录列表
     */
    @RequiresPermissions("score:professional:list")
    @PostMapping("/score/list")
    @ResponseBody
    public TableDataInfo scoreList(RecordProfessional recordProfessional)
    {
        startPage();
        List<RecordProfessional> list = recordProfessionalService.selectScoreProfessionalList(recordProfessional);
        return getDataTable(list);
    }

    /**
     * 职称申报得分
     */
    @GetMapping("/score/{professionalId}/{userType}")
    public String score(@PathVariable("professionalId") Long professionalId, @PathVariable("userType") String userType, ModelMap mmap)
    {
        RecordProfessional recordProfessional = recordProfessionalService.selectRecordProfessionalByProfessionalId(professionalId);
        mmap.put("recordProfessional", recordProfessional);
        mmap.put("userType", userType);
        return prefix + "/score/score";
    }

    /**
     * 修改保存职称申报
     */
    @RequiresPermissions("score:professional:save")
    @Log(title = "职称申报审批", businessType = BusinessType.UPDATE)
    @PostMapping("/score/save")
    @ResponseBody
    public AjaxResult saveSave(RecordProfessional recordProfessional)
    {
        return toAjax(recordProfessionalService.scoreRecordProfessional(recordProfessional));
    }

    @RequiresPermissions("review:professional:view")
    @GetMapping("/review")
    public String review()
    {
        return prefix + "/review/professional";
    }

    /**
     * 查询请假记录列表
     */
    @RequiresPermissions("review:professional:list")
    @PostMapping("/review/list")
    @ResponseBody
    public TableDataInfo reviewList(RecordProfessional recordProfessional)
    {
        startPage();
        List<RecordProfessional> list = recordProfessionalService.selectReviewProfessionalList(recordProfessional);
        return getDataTable(list);
    }

    /**
     * 修改职称申报
     */
    @GetMapping("/review/{professionalId}")
    public String review(@PathVariable("professionalId") Long professionalId, ModelMap mmap)
    {
        RecordProfessional recordProfessional = recordProfessionalService.selectRecordProfessionalByProfessionalId(professionalId);
        mmap.put("recordProfessional", recordProfessional);
        return prefix + "/review/review";
    }

    /**
     * 修改保存职称申报
     */
    @RequiresPermissions("review:professional:save")
    @Log(title = "职称申报评审", businessType = BusinessType.UPDATE)
    @PostMapping("/review/save")
    @ResponseBody
    public AjaxResult reviewSave(RecordProfessional recordProfessional)
    {
        return toAjax(recordProfessionalService.reviewRecordProfessional(recordProfessional));
    }
}
